﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace web.admin
{
    public partial class DistrictEdit : System.Web.UI.Page
    {
        public DistrictInfo Info =new DistrictInfo();
        public string Url_Back;
        public string name;
        public int ordervalue;
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = RequestTools.GetQueryInt("id", -1);
            if (id >= 0)
            {
                Info = DistrictBLL.Instance.GetDistrict(id);
                name = Info.Name;
                ordervalue = Info.OrderValue;
                if (Info.ParentId == 0)
                {
                    Url_Back = "ProvinceList.aspx";
                }
                else
                {
                    Url_Back = "CityList.aspx?parentId="+Info.ParentId;
                }
            }

        }
        protected void InsertButton_click(object sender, EventArgs e)
        {

            //数据有效性检查
            try
            {
                DistrictInfo info = validateForm(this.districtInfo1);

                if (info != null)
                {
                    //准备数据,调用SqlServerDAL执行增加
                    if (DistrictBLL.Instance.UpdateDistrict(info))  //成功
                    {
                        HttpContext.Current.Response.Redirect(Url_Back, true);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void CancelButton_click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect(Url_Back, true);
        }
        private DistrictInfo validateForm(DetailsView detailsView)
        {
            DistrictInfo _info = new DistrictInfo();
            _info.Name = ((TextBox)detailsView.FindControl("name")).Text;
            _info.OrderValue = Convert.ToInt32(((TextBox)detailsView.FindControl("ordervalue")).Text);
            _info.ParentId = Info.ParentId;
            _info.Id = Info.Id;
            return _info;
        }
    }
}
